<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Loader;

class LoginController extends Controller{
	public function index(){
		$dosubmit = input('post.dosubmit');
		if($dosubmit){
			$username = input('post.username');
			$password = input('post.password');

			if(!$username){
				$this->error('用户名不能为空');
			}
			if(!$password){
				$this->error('密码不能为空');
			}

			$info = DB::name('admin')->field('id,username,password')->where('username',$username)->find();
			dump($info);
			if(!$info){
				$this->error('用户不存在');
			}
			if(md5($password) != $info['password']){
				$this->error('密码不正确');
			}else{
				session('user_name',$info['username']);
				session('user_id',$info['id']);
				if(input('post.islogin')){
					cookie('user_name',encry_code($info['username']));
					cookie('user_id',encry_code($info['id']));
				}
				//Loader::model('Admin')->editInfo(1,$info['id']);
				$this->success("登入成功",'index/index');
			}
		}else{
			if(session('user_name')){
				$this->success('登入成功','index/index');
			}

			if(cookie('user_name')){
				$username = encry_code(cookie('user_name'),'DECODE');
				$info = DB::name('admin')->field('id,username,password')->where('username',$username)->find();
				if($info){
					session('username',$info['username']);
					session('user_id',$info['id']);
					//Loader::model('Admin')->editInfo(1,$info['id']);
					$this->success('登入成功','index/index');
				}
			}
				return $this->fetch('login');
		}
	}

	public function loginout(){
		session('user_name',null);
		session('user_id',null);
		cookie('user_name',null);
		cookie('user_id',null);
		$this->success('退出成功','login/index');
	}
}